package com.itheima.controller;

import com.itheima.util.R;
import com.itheima.domain.UmsMenu;
import com.itheima.manager.UmsMenuManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/menu")
public class UmsMenuController {

    @Autowired
    UmsMenuManager umsMenuManager;

    /**
     * 增加后台菜单
     *
     * @param umsMenu
     * @return
     */
    @PostMapping("/create")
    public R addCategory(@RequestBody UmsMenu umsMenu) {
        return umsMenuManager.addMenu(umsMenu);
    }

    /**
     * 根据id删除菜单
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R deleteMenu(@PathVariable Integer id) {
        return umsMenuManager.deleteMenu(id);
    }

    /**
     * 分页查询后台菜单
     *
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{parentId}")
    public R getByCondictions(@PathVariable Integer parentId,
                              @RequestParam(defaultValue = "1",required = false) Integer pageNum,
                              @RequestParam(defaultValue = "10",required = false) Integer pageSize) {
        return umsMenuManager.getByCondictions(parentId, pageNum, pageSize==5?10:pageSize);
    }

    /**
     * 修改菜单
     *
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateMenu(@PathVariable Integer id, @RequestBody UmsMenu umsMenu) {
        return umsMenuManager.updateMenu(id, umsMenu);
    }

    /**
     * 修改菜单状态
     *
     * @param id
     * @return
     */
    @PostMapping("/updateHidden/{id}")
    public R updateMenuStatu(@PathVariable Integer id, Integer hidden) {
        return umsMenuManager.updateMenuStatu(id, hidden);
    }

    /**
     * 根据id查询菜单
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectById(@PathVariable Integer id) {
        return umsMenuManager.selectById(id);
    }

    /**
     * 查询所有菜单
     * @return
     */
    @GetMapping("/treeList")
    public R selectMenuList() {

        return umsMenuManager.selectMenuList();
    }


}